package com.woniu.microcommunityserver.tribal.mapper;

import com.woniu.microcommunityserver.book.entity.BookInformationInfoEntity;
import com.woniu.microcommunityserver.tribal.entity.InterestTribalInfoEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

@Mapper
public interface InterestTribalInfoMapper {
    /**
     * 查询所有兴趣部落
     * @return
     */
    @Select("select id,tribal_name,fk_tribal_category_id,tribal_picture_storage_address," +
            "tribal_creation_time,tribal_state from interest_tribal_info")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="tribal_name",property="tribalName"),
            @Result(column="user_name",property="userName"),
            @Result(column="fk_tribal_category_id",property="fkTribalCategoryId"),
            @Result(column="tribal_picture_storage_address",property="tribalPictureStorageAddress"),
            @Result(column="tribal_creation_time",property="tribalCreationTime"),
            @Result(column="tribal_state",property="tribalState"),
            @Result(column="fk_tribal_category_id",property="tribalCategoryName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.TribalCategoryInfoMapper.findCategory",fetchType = FetchType.EAGER
                    ))
    })
    List<InterestTribalInfoEntity> findAll();

    /**
     * 查询指定兴趣部落
     * @param map
     * @return
     */
    @Select({"<script> " +
            "select id,tribal_name,fk_tribal_category_id,tribal_picture_storage_address," +
            "tribal_creation_time,tribal_state from interest_tribal_info" +
            "<where>" +
            "<if test='tribalName!= null'>" +
            "and tribal_name like concat('%', #{tribalName}, '%')"+
            "</if>"+
            "<if test='fkTribalCategoryId!= null'>" +
            "and fk_tribal_category_id = #{fkTribalCategoryId}"+
            "</if>"+
            "<if test='tribalState!= null'>" +
            "and tribal_state = #{tribalState}"+
            "</if>"+
            "</where>"+
            "</script>"})
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="tribal_name",property="tribalName"),
            @Result(column="user_name",property="userName"),
            @Result(column="fk_tribal_category_id",property="fkTribalCategoryId"),
            @Result(column="tribal_picture_storage_address",property="tribalPictureStorageAddress"),
            @Result(column="tribal_creation_time",property="tribalCreationTime"),
            @Result(column="tribal_state",property="tribalState"),
            @Result(column="fk_tribal_category_id",property="tribalCategoryName",javaType = String.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.tribal.mapper.TribalCategoryInfoMapper.findCategory",fetchType = FetchType.EAGER
                    ))
    })
    List<InterestTribalInfoEntity> findAny(Map<String,Object> map);

    /**
     * 新增兴趣部落
     * @param interestTribalInfoEntity
     * @return
     */
    @Insert("insert into interest_tribal_info(tribal_name,fk_tribal_category_id," +
            "tribal_picture_storage_address,tribal_creation_time,tribal_state) " +
            "values(#{i.tribalName},#{i.fkTribalCategoryId},#{i.tribalPictureStorageAddress}," +
            "#{i.tribalCreationTime},#{i.tribalState})")
    boolean insertInterest(@Param("i") InterestTribalInfoEntity interestTribalInfoEntity);

    /**
     * 修改兴趣部落
     * @param interestTribalInfoEntity
     * @return
     */
    @Update({"<script>",
            "update interest_tribal_info",
            "<set>",
            "<if test='tribalName!= null'>",
            "tribal_name = #{tribalName} ,",
            "</if>",
            "<if test='fkTribalCategoryId != null'>",
            "fk_tribal_category_id = #{fkTribalCategoryId} ,",
            "</if>",
            "<if test='tribalPictureStorageAddress != null'>",
            "tribal_picture_storage_address = #{tribalPictureStorageAddress} ,",
            "</if>",
            "<if test='tribalCreationTime != null'>",
            "tribal_creation_time = #{tribalCreationTime} ,",
            "</if>",
            "<if test='tribalState != null'>",
            "tribal_state = #{tribalState} ,",
            "</if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    boolean update(InterestTribalInfoEntity interestTribalInfoEntity);

    /**
     * 修改兴趣部落的状态
     * @param tribalState
     * @param id
     * @return
     */
    @Update("update interest_tribal_info set tribal_state=#{tribalState} where id=#{id}")
    boolean updateState(Integer tribalState,Long id);


    @Select("select tribal_name from interest_tribal_info where id=#{id}")
    String tribalName(Integer id);
}
